﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Photocopy.Model;
using Photocopy.Business;

namespace Photocopy
{
    public partial class Default1 : System.Web.UI.Page
    {
        ProductData pd = new ProductData();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["Product"] = null;
                RptHotProduct.DataSource = pd.getHotProduct();
                RptHotBuyProduct.DataSource = pd.getHotBuyProduct();
                RptHotProduct.DataBind();
                RptHotBuyProduct.DataBind();

                List<ProductRP> newlist = new List<ProductRP>();
                foreach (ProductRP p in pd.getSaleProduct())
                {
                    ProductRP newp = null;
                    newp = ReturnDiscount(p);
                    newlist.Add(newp);
                }
                RptSaleProduct.DataSource = newlist;
                RptSaleProduct.DataBind();
            }

            
        }

        //Hàm kiểm tra loại giảm giá, trả về sản phẩm với đơn giá mới
        public ProductRP ReturnDiscount(ProductRP p)
        {
                p.DonGiaMoi = Math.Round(p.DonGia - ((p.DonGia * p.GiaGiam) / 100),5);
                return p;
        }

        protected void RptHotProduct_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "GetProductDetail")
            {
                Label lblsanpham = (Label)e.Item.FindControl("lbsp1");
                int SanphamID = Int32.Parse(lblsanpham.Text);
                ProductData product = new ProductData();
                if(product.CheckSale(SanphamID) == true)
                {
                    Session["sanpham"] = product.getProductSale(SanphamID);
                    Response.Redirect("ProductDetails.aspx");
                }
                else
                {
                    Session["sanpham"] = product.getProduct(SanphamID);
                    Response.Redirect("ProductDetails.aspx");
                }
            }
        }

        protected void RptHotBuyProduct_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "GetProductDetail")
            {
                Label lblsanpham = (Label)e.Item.FindControl("lbsp2");
                int SanphamID = Int32.Parse(lblsanpham.Text);
                ProductData product = new ProductData();
                if (product.CheckSale(SanphamID) == true)
                {
                    Session["sanpham"] = product.getProductSale(SanphamID);
                    Response.Redirect("ProductDetails.aspx");
                }
                else
                {
                    Session["sanpham"] = product.getProduct(SanphamID);
                    Response.Redirect("ProductDetails.aspx");
                }
            }
        }

        protected void RptSaleProduct_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "GetProductDetail")
            {
                Label lblsanpham = (Label)e.Item.FindControl("lbsp3");
                int SanphamID = Int32.Parse(lblsanpham.Text);
                ProductData product = new ProductData();
                if (product.CheckSale(SanphamID) == true)
                {
                    Session["sanpham"] = product.getProductSale(SanphamID);
                    Response.Redirect("ProductDetails.aspx");
                }
                else
                {
                    Session["sanpham"] = product.getProduct(SanphamID);
                    Response.Redirect("ProductDetails.aspx");
                }
            }
        }

        protected void linkMore1_Click(object sender, EventArgs e)
        {
            Response.Redirect("HotProduct.aspx");
        }

    }
}